package com.demo.service.impl;


import java.util.Date;
import java.util.List;

import com.demo.entity.enums.*;
import com.demo.entity.po.UserInfo;
import com.demo.entity.po.Violation;

import com.demo.entity.query.UserInfoQuery;
import com.demo.entity.query.ViolationQuery;

import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.UserInfoMapper;
import com.demo.service.EmailCodeService;
import com.demo.service.ViolationService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.ViolationMapper;

/**
 * @Description:违规记录表serviceImpl
 * @Date:2025-03-01
 * @author：author
*/
@Service("violationService")
public class ViolationServiceImpl implements ViolationService{

	@Resource
	private ViolationMapper<Violation,ViolationQuery> violationMapper;

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;

	@Resource
	private EmailCodeService emailCodeService;

	/**
	 * 根据条件查询列表
	 */
	public List<Violation> findListByParam(ViolationQuery query) { 
		return this.violationMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(ViolationQuery query){
		return this.violationMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<Violation> findListByPage(ViolationQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<Violation> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(Violation bean) {
		// 校验类型
		ViolationTypeEnum typeEnum = ViolationTypeEnum.getByStatus(bean.getAction());
		if(typeEnum==null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		// 禁用修改用户状态
		if (typeEnum==ViolationTypeEnum.BAN){
			UserInfo updateInfo = new UserInfo();
			updateInfo.setStatus(UserStatusEnum.DISABLE.getStatus());
			userInfoMapper.updateByUserId(updateInfo,bean.getUserId());
		}
		String message=String.format("你最近的违规原因：%s,现在对你进行%s处理，处理时间%s，网络文明劝导不仅需要个人的努力，" +
				"还需要社会各界的共同参与。只有每个人都从自身做起，才能营造一个更加健康、和谐的网络环境。",
				bean.getReason(),bean.getAction()==ViolationTypeEnum.BAN.getType()?ViolationTypeEnum.BAN.getDesc():ViolationTypeEnum.WARING.getDesc(),bean.getCreateTime());
		UserInfo userInfoDB =userInfoMapper.selectByUserId(bean.getUserId());
		emailCodeService.sendEmail4Warn(userInfoDB.getEmail(),message);
		bean.setCreateTime(new Date());
		return this.violationMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<Violation> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.violationMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<Violation> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.violationMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据ViolationId查询
	 */
	public Violation getViolationByViolationId(Long violationId) { 
		return this.violationMapper.selectByViolationId(violationId);
	}

	/**
	 * 根据ViolationId更新
	 */
	public Integer updateViolationByViolationId(Violation bean, Long violationId) { 
		return this.violationMapper.updateByViolationId(bean,violationId);
	}

	/**
	 * 根据ViolationId删除
	 */
	public Integer deleteViolationByViolationId(Long violationId) { 
		return this.violationMapper.deleteByViolationId(violationId);
	}
}